package br.com.lazyprogrammer.spaceshooter.model.renderer;

import javax.microedition.khronos.opengles.GL10;

import br.com.lazyprogrammer.spaceshooter.manager.FontManager;
import br.com.lazyprogrammer.spaceshooter.manager.ImageManager;
import br.com.lazyprogrammer.spaceshooter.model.Player;
import br.com.lazyprogrammer.spaceshooter.model.World;
import br.com.lazyprogrammer.spaceshooter.model.meteor.BigBrown;
import br.com.limaogames.framework.gl.Camera2D;
import br.com.limaogames.framework.gl.SpriteBatcher;
import br.com.limaogames.framework.impl.GLGraphics;
import br.com.limaogames.framework.util.Display;

/**
 * Created by Andre on 20/08/2014.
 */
public class WorldRenderer {

    public static final float FRUSTUM_WIDTH = 10;
    public static final float FRUSTUM_HEIGHT = 15;
    GLGraphics glGraphics;
    World world;
    Camera2D cam;
    SpriteBatcher batcher;

    public WorldRenderer(GLGraphics glGraphics, SpriteBatcher batcher, World world) {
        this.glGraphics = glGraphics;
        this.world = world;
        this.cam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
        this.batcher = batcher;
    }

    public void render() {
        cam.setViewportAndMatrices();
        renderBackground();
        renderObjects();
        drawScore();
    }

    public void drawScore() {
        GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        batcher.beginBatch(FontManager.fonts);
        FontManager.font.drawText(batcher, "score: " + world.score, 0.5f, 14.5f, 0.5f, 0.625f);
        batcher.endBatch();
        gl.glDisable(GL10.GL_BLEND);
    }

    public void renderBackground() {
        batcher.beginBatch(ImageManager.backgroundPurple);
        batcher.drawSprite(cam.position.x, cam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, ImageManager.bgPurple);
        batcher.endBatch();
    }

    public void renderObjects() {
        GL10 gl = glGraphics.getGL();
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        renderShots();
        renderShips();
        renderMeteors();
        gl.glDisable(GL10.GL_BLEND);
    }

    public void renderShips() {
        batcher.beginBatch(ImageManager.ships);
        batcher.drawSprite(world.player.position.x, world.player.position.y, Player.SHIP_WIDTH, Player.SHIP_HEIGHT, world.player.angle - 90, ImageManager.shipPlayerBlue01);
        batcher.endBatch();
    }

    public void renderMeteors() {
        batcher.beginBatch(ImageManager.meteors);
        for (int i = 0; i < world.meteors.size(); i++) {
            //if (world.meteors.get(i) instanceof BigBrown)
                batcher.drawSprite(world.meteors.get(i).position.x, world.meteors.get(i).position.y, BigBrown.METEOR_WIDTH, BigBrown.METEOR_HEIGHT, ImageManager.meteorBigBrown01);
        }
        batcher.endBatch();
    }

    public void renderShots() {
        if (world.shots.size() == 0)
            return;
        batcher.beginBatch(ImageManager.ships);
        for (int i = 0; i < world.shots.size(); i++) {
            batcher.drawSprite(world.shots.get(i).position.x, world.shots.get(i).position.y, 1, 1, ImageManager.shipPlayerBlue04);
        }
        batcher.endBatch();
    }

}
